#include <iostream>

#include "./myrpc.h"
#include "xlcomm/net/event_loop_thread.h"
#include "xlcomm/net/rpc/rpc_client.h"
using namespace xlcomm;

void AddCallback(const net::RpcParamReader& resReader,
                 net::RpcErrorCode errcode) {
  std::cout << "----AddCallback---- \n";
  MathResp resp = {0};
  resReader.getItemData(resp);
  std::cout << resp.result << std::endl;
}

int main() {
  // Logger::SetLogLevel(Logger::kTrace);
  net::EventLoopThread loop_thread;
  net::EventLoop* loop = loop_thread.StartLoop();
  net::RpcClient client(loop, net::InetAddress(40003, "127.0.0.1"));
  client.Connect();
  std::this_thread::sleep_for(std::chrono::seconds(1));

  MathReq req;
  net::RpcParam param;
  req.a = 3;
  req.b = 2;
  param << req;
  client.AsyncCall("MathService", "Add", param,
                   std::bind(&AddCallback, _1, _2));

  net::RpcParam param2;
  req.a = 3;
  req.b = 1;
  param2 << req;
  client.AsyncCall(
      "MathService", "Sub", param2,
      [](const net::RpcParamReader& resReader, net::RpcErrorCode errcode) {
        std::cout << "----lamda---- \n";
        MathResp resp = {0};
        resReader.getItemData(resp);
        std::cout << resp.result << std::endl;
      });

  std::this_thread::sleep_for(std::chrono::seconds(2));
  client.Disconnect();
  std::this_thread::sleep_for(std::chrono::seconds(1));
}